package co.demo;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.MarshalBase64;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Environment;
import android.widget.TextView;

public class main extends Activity {

//	private static final String METHOD_NAME = "CelsiusToFahrenheit";
//	private static final String METHOD_NAME = "playTheGame";
	private static final String METHOD_NAME = "getbytes";
//	com.mcnz.rps.RPSService.playTheGame
//	private static final String SOAP_ACTION = "http://tempuri.org/CelsiusToFahrenheit";
//	private static final String SOAP_ACTION = "http://192.168.0.13:8080/RPSService/playTheGame";
	private static final String SOAP_ACTION = "http://192.168.0.13:8080/RPSService/getbytes";
	
	
//	private static final String NAMESPACE = "http://192.168.0.13:8080/";
		private static final String NAMESPACE = "http://rps.mcnz.com/";
	
//	private static final String URL = "http://www.w3schools.com/webservices/tempconvert.asmx";
	private static final String URL = "http://192.168.0.13:8080/RPSService/RPSService?wsdl";
	

	TextView tv;

	
	public String testWebService(Bitmap bmp) {

	    MarshalBase64 marshal = new MarshalBase64();
	    ByteArrayOutputStream out = new ByteArrayOutputStream();
	    bmp.compress(CompressFormat.JPEG, 100, out);
	    byte[] raw = out.toByteArray();

	    SoapObject request =new SoapObject(NAMESPACE, METHOD_NAME);

	    request.addProperty("image", raw);

	    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
	        SoapEnvelope.VER11);
//	    envelope.dotNet = true;
	    envelope.setOutputSoapObject(request);
	    marshal.register(envelope);
	    HttpTransportSE httpTransport = new HttpTransportSE(URL);
	    httpTransport.debug=true;
	    try

	    {

	        httpTransport.call(SOAP_ACTION, envelope);
	        
	    	String req=httpTransport.requestDump;
			String res=httpTransport.responseDump;
			
	    	SoapPrimitive sp =(SoapPrimitive)envelope.getResponse();
		
		return sp.toString();
			
	  }

	    catch (Exception exception)

	    {
	        exception.printStackTrace();

	    }
return null;
	    }

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		tv=(TextView)findViewById(R.id.textView1);
		
		String filepath="";
		filepath = Environment.getExternalStorageDirectory()+"/captureInfo.jpg";
		
		Bitmap b=BitmapFactory.decodeFile(filepath);
		tv.setText("Status : "+testWebService(b));
//		
//		SoapObject request=new SoapObject(NAMESPACE, METHOD_NAME);
////		request.addProperty("Celsius", "23");
//		request.addProperty("clientChoice", "paper");
//		
//		SoapSerializationEnvelope soapEnvelope =new SoapSerializationEnvelope(SoapEnvelope.VER11);
//		
////		soapEnvelope.dotNet=true;
//		soapEnvelope.setOutputSoapObject(request);
//		
//		AndroidHttpTransport aht =new AndroidHttpTransport(URL);
//		aht.debug = true;
//		
//		try {
//			aht.call(SOAP_ACTION, soapEnvelope);
//			String req=aht.requestDump;
//			String res=aht.responseDump;
//			SoapPrimitive sp =(SoapPrimitive)soapEnvelope.getResponse();
//			tv.setText("Status : "+sp);
//			
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (XmlPullParserException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		

	}
}